package com.gang.ssm.bos.system.controller;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.gang.ssm.bos.common.Page;
import com.gang.ssm.bos.core.anotation.Permission;
import com.gang.ssm.bos.core.constant.Cst;
import com.gang.ssm.bos.core.toolbox.ajax.AjaxResult;
import com.gang.ssm.bos.core.toolbox.file.BladeFile;
import com.gang.ssm.bos.core.toolbox.file.BladeFileKit;
import com.gang.ssm.bos.core.toolbox.file.FileRender;
import com.gang.ssm.bos.core.toolbox.kit.PathKit;
import com.gang.ssm.bos.system.bean.Role;
import com.gang.ssm.bos.system.bean.User;
import com.gang.ssm.bos.system.service.RoleService;
import com.gang.ssm.bos.system.service.UserService;

@RequestMapping("/user")
@Controller
public class UserController extends BosController {
	
	/**
	 * 图片类型
	 */
	private static final String IMAGE = "image";
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private RoleService roleService;
	
	@RequestMapping("/list")
	public String list(Page<User> page,Map<String, Object> map) {
		
		userService.pageQuery(page);
		map.put("page", page);
		return "system/user/userList";
	}
	
	@RequestMapping("/user")
	public String input(Integer id, Map<String, Object> map) {
		
		List<Role> roleList = roleService.findAll();
		
		List<Integer> roleIds = null;
		User user = null;
		if(id != null) {
			
			user = userService.findById(id);
			roleIds = roleService.findIdListByUserId(id);
		}
		
		if(roleIds != null) {
			for (Role role : roleList) {
				if(roleIds.contains(role.getId())) {
					role.setUserHas(true);
				}
			}
		}
		map.put("user", user);
		map.put("roleList", roleList);
		
		return "system/user/user";
	}
	
	@Permission({ADMIN})
	@ResponseBody
	@RequestMapping("/save")
	public AjaxResult save(User user, String roleIds,
			MultipartFile userPhoto, HttpServletRequest request) {
		
		if(!userPhoto.isEmpty()) {
			String originalFileName = userPhoto.getOriginalFilename();
			boolean ok = BladeFileKit.testExt(IMAGE, originalFileName);
			if(!ok) {
				return fail("上传文件的类型不允许");
			}
			BladeFile bf = getFile(userPhoto);
			bf.transfer(false);
			
			user.setHeadImage(bf.getUploadVirtualPath());
		}
		
		
		try {
			userService.saveOrUpdate(user, roleIds);
			return success("保存成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fail("未知错误,保存失败");	
	}
	
	@Permission({ADMIN})
	@ResponseBody
	@RequestMapping("/delete")
	public AjaxResult delete(Integer id) {
		
		try {
			userService.deleteById(id);
			return success("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fail("删除失败");
	}
	
	@RequestMapping("/renderFile")
	public void renderFile(HttpServletRequest request, HttpServletResponse response, String url) {
		//String url = file.get("URL").toString();
		File f = new File((Cst.me().isRemoteMode() ? "" : PathKit.getWebRootPath()) + url);
		FileRender.init(request, response, f).render();
	}
	
}
